rm(list = ls())
set.seed(1234567)
# Load useful functions ---------------------------------------------------

source("02_code/00_useful_functions/variable_building_functions.R")


# Make codebooks ----------------------------------------------------------

# Codebooks are R list objects that correspond to each question in the 
# survey. For example, mlcb$sex_q2_1 gives the labels corresponding to 
# variable sex_q2_1 in the adult household survey

source("02_code/01_codebook/make_codebooks.R")

# Load data and packages --------------------------------------------------

# - find any packages that are needed and install them if not installed
# - load the different datasets used in the analysis

source("02_code/02_load_and_clean_data/00_load_packages_and_data.R")

# Building data for analysis ----------------------------------------------

rerun_data_building <- TRUE

if(rerun_data_building){
	
	# Clean data --------------------------------------------------------------
	
	# These scripts implement the cleaning of data conducted in the field by 
	# the field manager.
	
	# FIELD EXPERIMENT
	
	# Sampling Radius Data
	source("02_code/02_load_and_clean_data/01_clean_sampling_radius_data.R")
	
	# Intervention data 
	source("02_code/02_load_and_clean_data/01_clean_festival_data.R")
	
	# Household data
	source("02_code/02_load_and_clean_data/01_clean_household_data_midline.R")
	source("02_code/02_load_and_clean_data/01_clean_household_data_endline.R")
	
	# VHT data
	source("02_code/02_load_and_clean_data/01_clean_vht_data_midline.R")
	source("02_code/02_load_and_clean_data/01_clean_vht_data_endline.R")
	
	# LAB EXPERIMENT
	source("02_code/02_load_and_clean_data/01_clean_lab_data.R")
	
	# PILOT EXPERIMENT
	
	# Intervention data
	source("02_code/02_load_and_clean_data/01_clean_festival_data_pilot.R")
	
	# Household data
	source("02_code/02_load_and_clean_data/01_clean_household_data_pilot.R")


	# Code variables field experiment midline ---------------------------------

	# These scripts create the outcomes and covariates used in the analysis
	
	# Household survey 
	source("02_code/03_variable_coding/01_code_covariates_hh_midline.R")
	source("02_code/03_variable_coding/01_code_outcomes_hh_midline.R")
	source("02_code/03_variable_coding/01_code_outcomes_hh_midline_randomized.R")
	
	# VHT Survey
	source("02_code/03_variable_coding/01_code_variables_vht_midline.R")
	
	# Build lists of variable names
	source("02_code/03_variable_coding/02_build_variable_lists_midline.R")
	
	
	# Save non-imputed midline data
	ml_no_imp <- ml
	
	# Impute variables field experiment midline -------------------------------

	# Here we use multiple chained imputation as pre-registered to impute 
	# item-level missingness in the midline.
	
	# VHT Survey
	vht_ml <- arrange(vht_ml, KEY)
	source("02_code/03_variable_coding/03_impute_variables_vht_midline.R")
	
	# Household Survey
	ml <- arrange(ml, KEY)
	source("02_code/03_variable_coding/03_impute_variables_hh_midline.R")
	
	
	# Merging field experiment data -------------------------------------------

	# These scripts merge midline and endline surveys from the field experiment, and also bring in  cluster-level covariates into the field experiment household data.
	
	# Merge cluster-level covariates from VHT survey into household data
	source("02_code/04_merging/01_merge_cluster_covariates_field_experiment.R")
	
	# Merge midline and endline household surveys 
	source("02_code/04_merging/02_merge_midline_endline_field_experiment.R")
	
	# Merge trading center level variables into other datasets
	source("02_code/04_merging/03_merge_treatment_and_festival_data_field_experiment.R")

	# Code variables field experiment endline --------------------------------------------------
	
	# These scripts build variables used in the analysis of the field experiment endline data.
	
	# Household Survey
	source("02_code/03_variable_coding/01_code_outcomes_hh_endline.R")
	source("02_code/03_variable_coding/01_code_compliance_related_hh_endline.R")
	source("02_code/03_variable_coding/01_code_covariates_hh_endline.R")
	
	# These questions were randomized and so need to be re-merged
	source("02_code/03_variable_coding/01_code_outcomes_hh_endline_randomized.R")
	
	# Vht survey 
	source("02_code/03_variable_coding/01_code_covariates_vht_endline.R")
	source("02_code/03_variable_coding/01_code_outcomes_vht_endline.R")
	
	# Build lists of variable names for use in imputation and in analysis
	source("02_code/03_variable_coding/02_build_variable_lists_endline.R")
	
	# Save non-imputed endline data
	el_no_imp <- el
	
	# Impute variables field experiment endline -------------------------------
	
	# Here we use multiple chained imputation as pre-registered to impute 
	# item-level missingness in the endline.
	
	# Household Survey
	el <- arrange(el,KEY)
	source('02_code/03_variable_coding/03_impute_variables_hh_endline.R')
	
	# Build indices field experiment ------------------------------------------

	# Building index variables from imputed components in the field experiment data
	
	source("02_code/03_variable_coding/04_build_indices.R")
	
	# Code variables lab experiment -------------------------------------------
	
	source("02_code/03_variable_coding/01_code_outcomes_lab.R")
	source("02_code/03_variable_coding/01_code_outcomes_lab_randomized.R")
	source("02_code/03_variable_coding/01_code_covariates_lab.R")
	source("02_code/03_variable_coding/01_code_treatment_assignment_lab.R")

	# Build lists of variable names for use in imputation and in analysis
	source("02_code/03_variable_coding/02_build_variable_lists_lab.R")
	
	# Impute variables lab experiment -----------------------------------------
	
	# Here we use multiple chained imputation as pre-registered to impute 
	# item-level missingness in the lab experiment data
	
	lab <- arrange(lab,KEY)
	source('02_code/03_variable_coding/03_impute_variables_lab.R')
	
	# Build indices lab experiment ------------------------------------------
	
	# Building index variables from imputed components in the lab experiment data
	source("02_code/03_variable_coding/04_build_indices_lab.R")
	

	# Merge lab data and trading center information ---------------------------
	source("02_code/04_merging/04_merge_in_block_data_lab.R")
	
	# Code and impute pilot data ----------------------------------------------

	source("02_code/03_variable_coding/05_code_and_impute_pilot_data.R")
	
	# Merge pilot data and pilot treatment assignment -------------------------------

	source("02_code/04_merging/05_merge_treatment_and_festival_data_pilot.R")
	
	# Save data image ---------------------------------------------------------
	
	save.image(file = "01_data/UG_absenteeism_data.Rdata")

} else {
	
	load(file = "01_data/UG_absenteeism_data.Rdata")
	
}

# Select covariates field experiment -------------------------------------------------------

# Load custom-written lasso selection functions
source("02_code/00_useful_functions/lasso_functions.R")

# Set to TRUE to rerun lasso selection
rerun_lasso <- TRUE 
source("02_code/05_covariate_selection/01_select_covariates.R")

# Run analyses ------------------------------------------------------------

# Load relevant functions
source("02_code/00_useful_functions/analysis_functions.R")
source("02_code/00_useful_functions/randomization_functions.R")

# Setting the number of sims for all analyses
sims <- 3000
# Setting the number of sims for multiple comparisons simulations
sims_mc <- 10000

# Main text ---------------------------------------------------------------

rerun_main_analyses <- TRUE

if(rerun_main_analyses) {
	# Table 1 - lab experiment
	source("02_code/06_analyses/main_text/conative_attitude_matrix_lab.R")
	
	# Table 2 - lab experiment
	source("02_code/06_analyses/main_text/conative_attitudes_lab_treatment_assigned.R")
	
	# Table 3 - field experiment
	source("02_code/06_analyses/main_text/descriptive_statistics.R")
	
	# Table 4 - field experiment
	source("02_code/06_analyses/main_text/conative_attitudes_compliers.R")
	
	# Table 5 - field experiment
	source("02_code/06_analyses/main_text/norms_and_efficacy.R")
	
	# Table 6 - field experiment
	source("02_code/06_analyses/main_text/salience_and_importance.R")
	
	# Table 7 - field experiment
	source("02_code/06_analyses/main_text/levels_of_absenteeism.R")
	
	# Descriptive statistics in text
	source("02_code/06_analyses/main_text/descriptive_statistics_in_text.R")
}

# Supplementary material --------------------------------------------------

rerun_appendix_analyses <- TRUE

if(rerun_appendix_analyses){
	
	# Figure B4 - field experiment
	source("02_code/06_analyses/supplementary_material/attendance_plot.R")
	
	# Table C2 - lab experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_lab.R")
	
	# Table C3 -  Lab experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_lab_treatment_assigned_fixed_effects.R")
	
	# Table C4 -  Lab experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_lab_treatment_assigned_covariates.R")
	
	# Table C5 - Lab experiment
	source("02_code/06_analyses/supplementary_material/norms_and_efficacy_lab.R")
	
	# Table D1 - field experiment
	source("02_code/06_analyses/supplementary_material/el_balance_all.R")
	
	# Table D2 - field experiment
	source("02_code/06_analyses/supplementary_material/el_balance_compliers.R")
	
	# Table D3 - field experiment
	source("02_code/06_analyses/supplementary_material/ml_balance_all.R")
	
	# Table D4 - field experiment
	source("02_code/06_analyses/supplementary_material/ml_balance_compliers.R")
	
	# Tables D5 - D8 - field experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_matrix_field_experiment.R")
	
	# Table D9 - field experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_all_compliers.R")
	
	# Table D10 - field experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_passiveoptions.R")
	
	# Table D11 - field experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_no_audience_size.R")
	
	# Table D12 - field experiment
	source("02_code/06_analyses/supplementary_material/conative_attitudes_lasso.R")
	
	# Table D13 - field experiment 
	source("02_code/06_analyses/supplementary_material/conative_attitudes_spillovers.R")
	
	# Table D14 - field experiment
	source("02_code/06_analyses/supplementary_material/additional_outcomes.R")

	# Table D15 - field experiment
	# Rerun with care: The labeling of rows of the resulting table will not be correct if ordering of p-values changes.
	source("02_code/06_analyses/supplementary_material/multiple_comparisons.R")
	
	# Table D16 - field experiment
	source("02_code/06_analyses/supplementary_material/multiple_comparisons_BH.R")
	
	# Table D17 - field experiment
	# Rerun with care: The labeling of rows of the resulting table will not be correct if ordering of p-values changes.
	source("02_code/06_analyses/supplementary_material/multiple_comparisons_absenteeism.R")
	
	# Table D18 - field experiment
	source("02_code/06_analyses/supplementary_material/multiple_comparisons_absenteeism_BH.R")
	
	# Table E1 - pilot experiment
	source("02_code/06_analyses/supplementary_material/pilot_field_experiment_analysis.R")
	
	# Table E2 - E3 -  pilot & main field experiment
	source("02_code/06_analyses/supplementary_material/meta_analysis.R")
		
	# Table F1 - Power calculation stepped-wedge design
	source("02_code/06_analyses/supplementary_material/power_analysis_stepped_wedge.R")
	
	
}
